<template>
  <el-input v-show="isShow" size="small" v-model="value" :name="field.vModel" :clearable="clearable" :disabled="isDisabled" :placeholder="field.fieldRemark" class="input-with-select">
    <el-button id="selectId" v-if="!isReadOnly" slot="append" @click="onCustomDialogClick()" type="primary" icon="el-icon-search"></el-button>
  </el-input>
</template>

<script>
  export default {
    name: "f2bpm-customdialog",
    props: ['field', 'fieldvalue'],
    data: function () {
      return {
        isView: false,
        isShow: true,
        isReadOnly: false,
        inputCtrlOption: null,
        customJSON: null,

        isDisabled: false,
        selectId: '',
        data: {},
        value: '',
        clearable: true
      };
    },
    watch: {
      value(val, oleV) {
        var that = this;
        if (val != undefined && oleV != undefined && f2grid_global_mainVueLoadComplated) {
          this.$emit('updatevalue', that.field, val);
        }
      },
      fieldvalue: {
        handler(newV, oleV) {
          var that = this;
          if (newV != undefined && oleV != undefined && f2grid_global_mainVueLoadComplated) {
            that.value = newV;
          }
        },
        immediate: false
      }
    },
    created: function () {
      var that = this;
      that.initailFieldOption();
      var formAction = WFContext.WorkflowformAction;
      if (formAction > 2) {
        that.isReadOnly = true;
      } else if (that.field._isReadOnly == true) {
        that.isReadOnly = true;
      }
      that.value = that.fieldvalue;
      if (that.field.inputCtrlOption) {
        var json = typeof that.field.inputCtrlOption === 'object' ? that.field.inputCtrlOption : eval('(' + that.field.inputCtrlOption + ')');
        that.inputCtrlOption = json;
      }
      that.init();
    },
    mounted() {
      var that = this;
      //FUI.F2Grid.excuteFieldWritePowerByVueFormItem(that);
      if (that.isReadOnly == true) {
        that.isDisabled = true;
      }
    },
    methods: {
      initailFieldOption() {
        var that = this;
        if (that.field.fieldOptions) {
          var fieldOptions = typeof that.field.fieldOptions === 'string' ? eval('(' + that.field.fieldOptions + ')') : that.field.fieldOptions;
          var general = fieldOptions.find(a => a.opttype == 'general');
          if (general) {
            var kvData = general.params.kvData;
            var item = kvData.find(a => a.key == 'clearable');
            if (item) {
              that.clearable = item.value;
            }
          }
        }
      },
      init() {
        var that = this;
        var name = that.field.vModel;
        var field = that.field.vModel;
        var defaultvalue = that.field.fieldDefaultValue || '';
        if (that.inputCtrlOption) {
          var customJSON = that.inputCtrlOption.data;
          that.customJSON = customJSON;
          var isEnableEdit = customJSON.isEnableEdit;
          that.isMulti = customJSON.isMulti;
          if (isEnableEdit == undefined || isEnableEdit == null) {
            isEnableEdit = true;
          }
          var selectId = Object.toGuid();
          that.selectId = selectId;
          if (WFContext && WFContext.WorkflowformAction && WFContext.WorkflowformAction > 2) {
            that.isReadOnly = true;
            that.isDisabled = true;
          }
          if (Object.toQueryString('formAction') && isNaN(Object.toQueryString('formAction')) && Object.toQueryString('formAction') != 'Add' && Object.toQueryString('formAction') != 'Edit') {
            that.isReadOnly = true;
            that.isDisabled = true;
          }
          if (WFContext.WorkflowformAction == 0 && defaultvalue) {
            var myValue = getSysDefaultValue(defaultvalue);
            that.value = myValue;
          }
          if (isEnableEdit == false || isEnableEdit == 'false') {
            // 只能选，不能对输入框进行输入
            that.isDisabled = true;
          }
          if (that.isReadOnly) {
            that.isDisabled = true;
          }
        }
      }
      //获取全部配置参数
      , getParms(inputCtrlOptionJson) {
        var that = this;
        var parms = {};
        var adParms = that.getAdParms(inputCtrlOptionJson);
        if (adParms && adParms.length > 0) {
          $.each(adParms, function (i, item) {
            parms[item.key] = item.value;
          });
        }
        var formFieldParms = that.getFormFieldParams(inputCtrlOptionJson);
        if (formFieldParms && formFieldParms.length > 0) {
          $.each(formFieldParms, function (i, item) {
            parms[item.key] = item.value;
          });
        }
        return parms;
      }
      //获取全部配置参数{a:b}
      , getParms(inputCtrlOptionJson) {
        var that = this;
        var parms = {};
        var adParms = that.getAdParms(inputCtrlOptionJson);
        if (adParms && adParms.length > 0) {
          $.each(adParms, function (i, item) {
            parms[item.key] = item.value;
          });
        }
        var formFieldParms = that.getFormFieldParams(inputCtrlOptionJson);
        if (formFieldParms && formFieldParms.length > 0) {
          $.each(formFieldParms, function (i, item) {
            parms[item.key] = item.value;
          });
        }
        return parms;
      }
      //获取高级参数：[{'key':'A','value':'1','orderNum':1}]
      , getAdParms(inputCtrlOptionJson) {
        var that = this;
        var parmsArr = inputCtrlOptionJson.data.parms || [];
        if (parmsArr && typeof parmsArr === 'string') {
          parmsArr = eval('(' + parmsArr + ')');
        }
        return parmsArr;
      }
      //获取表单字段参数[{key:"",value:""}];
      , getFormFieldParams(inputCtrlOptionJson) {
        var that = this;
        //携带表单字段做为变量参数（需要在自定义对话框的过滤条件中配置对应的变量表达式，字段名为变量名）
        var formFieldParams = inputCtrlOptionJson.data.formFieldParams || "";
        var arrs = [];
        if (formFieldParams) {
          arrs = eval('(' + formFieldParams + ')');
        }
        var varParams = [];
        $.each(arrs, function (i, kvItem) {
          let key=kvItem.key;
          let fieldName=kvItem.value;
          var isSub = fieldName.split(".").length >= 3;
          var value = isSub == false ? myvm.data[fieldName] : that.$parent.data[fieldName];
          varParams.push({ "key": key, "value": value });
        });
        return varParams;
      }
      , onCustomDialogClick() {
        var that = this;
        var isPass = true;
        var fieldDef = that.field;
        var funcName_before = fieldDef.vModel.replace(/\./g, '_') + "_before";
        if (Object.toIsExitsFunction(funcName_before)) {
          isPass = window[funcName_before](that.value, fieldDef);
        }
        if (isPass == false) {
          return false;
        }
        if (that.inputCtrlOption) {
          var customJSON = that.inputCtrlOption.data;
          customJSON.selectId = that.selectId;
          var fieldName = that.field.vModel;
          customJSON.backBindIsdetail = fieldName.split('.').length >= 3;
          if (customJSON.isMulti == 'true') {
            customJSON.isMulti = true;
          }
          if (customJSON.isMulti == 'false') {
            customJSON.isMulti = false;
          }
          var varParams = that.getFormFieldParams(that.inputCtrlOption);
          if (varParams.length > 0) {
            customJSON.varParams = varParams;
          }
          var customDialogName = customJSON.customDialog;
          var customDialog = eval(CustomDialog);
          customDialog(that, customJSON);
        }
        return false;
      },
      onSelectedSuccess(bindFieldData) {
        var that = this;
        var isSubtable = that.field.vModel.split('.').length >= 3;
        $.each(bindFieldData, function (tofieldName, pval) {
          if (tofieldName == that.field.vModel) {
            that.value = pval;
            that.onBlur();
          } else {
            // 绑定当前数据域对象的其它字段
            that.$emit('updatevalue', tofieldName, pval);
          }
        });
      },
      onBlur() {
        var that = this;
        var fieldDef = that.field;
        var fieldValue = that.value;
        var funcName = fieldDef.vModel.replace(/\./g, '_');
        if (Object.toIsExitsFunction(funcName)) {
          window[funcName](fieldValue, fieldDef);
        }
      }
    }
  }
</script>